package com.yt.order.service;

import com.yt.order.models.vo.DispatchRuleConfigPageVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.order.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.order.mapper.DispatchRuleConfigMapper;
import com.yt.order.models.service.IDispatchRuleConfigService;
import com.yt.order.models.bo.DispatchRuleConfigSaveBO;
import com.yt.order.models.dto.DispatchRuleConfigDTO;
import com.yt.order.models.service.IDispatchRuleFilterService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yt.order.models.bo.DispatchRuleFilterSaveBO;
import com.yt.order.models.dto.DispatchRuleFilterDTO;
import com.yt.order.models.vo.DispatchRuleConfigVO;
import com.baomidou.mybatisplus.plugins.Page;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 派单规则信息控制器
 *
 * @author yzz
 * @Date 2025-09-03 14:17:40
 */
@DubboService
public class DispatchRuleConfigServiceImpl extends ServiceImpl<DispatchRuleConfigMapper, DispatchRuleConfigDTO> implements IDispatchRuleConfigService{

    @Resource
    private YmlConfig ymlConfig;
    
    @DubboReference
    private IDispatchRuleFilterService dispatchRuleFilterService;

    @Override
    public boolean add(DispatchRuleConfigSaveBO saveBO) {
        DispatchRuleConfigDTO dto = new DispatchRuleConfigDTO();
        
        // 打印调试信息，检查传入的saveBO对象
        System.out.println("Received saveBO: " + saveBO);
        
        // 手动设置各个属性，确保数据正确复制
        if (saveBO.getRuleName() != null) {
            dto.setRuleName(saveBO.getRuleName());
        }
        if (saveBO.getType() != null) {
            dto.setType(saveBO.getType());
        }
        if (saveBO.getStatus() != null) {
            dto.setStatus(saveBO.getStatus());
        }
        if (saveBO.getPriority() != null) {
            dto.setPriority(saveBO.getPriority());
        }
        if (saveBO.getMatchMode() != null) {
            dto.setMatchMode(saveBO.getMatchMode());
        }
        if (saveBO.getRuleDesc() != null) {
            dto.setRuleDesc(saveBO.getRuleDesc());
        }
        if (saveBO.getRevision() != null) {
            dto.setRevision(saveBO.getRevision());
        }
        
        // 设置创建时间和更新时间
        Date now = new Date();
        dto.setCreateTime(now);
        dto.setUpdateTime(now);
        
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        
        // 打印调试信息，检查要保存的dto对象
        System.out.println("Saving dto: " + dto);
        
        // 保存规则配置
        boolean result = this.insert(dto);
        
        // 如果规则保存成功且有过滤条件，则保存过滤条件
        if (result && saveBO.getFilters() != null && !saveBO.getFilters().isEmpty()) {
            for (DispatchRuleFilterSaveBO filterSaveBO : saveBO.getFilters()) {
                DispatchRuleFilterDTO filterDTO = new DispatchRuleFilterDTO();
                BeanUtils.copyProperties(filterSaveBO, filterDTO);
                filterDTO.setRuleId(id); // 设置关联的规则ID
                filterDTO.setId(ymlConfig.getPkId()); // 生成新的ID
                filterDTO.setCreateTime(now);
                filterDTO.setUpdateTime(now);
                dispatchRuleFilterService.insert(filterDTO);
            }
        }
        
        return result;
    }
    
    @Override
    public boolean deleteById(Serializable id) {
        // 先删除相关的派单条件过滤信息
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("rule_id", id);
        dispatchRuleFilterService.delete(wrapper);
        
        // 再删除主记录
        return super.deleteById(id);
    }
    
    @Override
    public List<DispatchRuleConfigVO> selectListWithFilters() {
        // 获取所有派单规则
        List<DispatchRuleConfigDTO> configList = this.selectList(null);
        
        // 转换为VO对象并关联过滤条件
        return configList.stream().map(config -> {
            DispatchRuleConfigVO vo = new DispatchRuleConfigVO();
            BeanUtils.copyProperties(config, vo);
            
            // 查询关联的过滤条件
            Wrapper<DispatchRuleFilterDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("rule_id", config.getId());
            List<DispatchRuleFilterDTO> filterList = dispatchRuleFilterService.selectList(wrapper);
            vo.setFilters(filterList);
            
            return vo;
        }).collect(Collectors.toList());
    }
    
    @Override
    public DispatchRuleConfigVO selectByIdWithFilters(Long id) {
        // 获取指定ID的派单规则
        DispatchRuleConfigDTO config = this.selectById(id);
        if (config == null) {
            return null;
        }
        
        // 转换为VO对象并关联过滤条件
        DispatchRuleConfigVO vo = new DispatchRuleConfigVO();
        BeanUtils.copyProperties(config, vo);
        
        // 查询关联的过滤条件
        Wrapper<DispatchRuleFilterDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("rule_id", id);
        List<DispatchRuleFilterDTO> filterList = dispatchRuleFilterService.selectList(wrapper);
        vo.setFilters(filterList);
        
        return vo;
    }
    
    @Override
    public DispatchRuleConfigPageVO selectPageWithFilters(Page<DispatchRuleConfigDTO> page, Wrapper<DispatchRuleConfigDTO> wrapper) {
        // 执行分页查询
        Page<DispatchRuleConfigDTO> configPage = this.selectPage(page, wrapper);
        
        // 创建VO分页对象
        DispatchRuleConfigPageVO voPage = new DispatchRuleConfigPageVO();
        BeanUtils.copyProperties(configPage, voPage, "records");
        
        // 转换数据列表
        List<DispatchRuleConfigVO> voList = configPage.getRecords().stream().map(config -> {
            DispatchRuleConfigVO vo = new DispatchRuleConfigVO();
            BeanUtils.copyProperties(config, vo);
            
            // 查询关联的过滤条件
            Wrapper<DispatchRuleFilterDTO> filterWrapper = new EntityWrapper<>();
            filterWrapper.eq("rule_id", config.getId());
            List<DispatchRuleFilterDTO> filterList = dispatchRuleFilterService.selectList(filterWrapper);
            vo.setFilters(filterList);
            
            return vo;
        }).collect(Collectors.toList());
        
        voPage.setRecords(voList);
        return voPage;
    }
}